package dao;

import entity.User;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class UserDao {
    List<User> list= UserDB.getInstance();
    List listlogin= iflogin.getInstance();
    public int addUser(User user){

       list.add(user);
        return 0;
    }
    public boolean checklogin(){
        for(int i=0;i<listlogin.size();i++){
            if(listlogin.get(i).equals(1))
                return false;
        }
        return true;
    }
    public boolean checkname(String name) {
        int flag=1;

        for (User user : list) {
            if (user.name.equals(name))
                flag=0;
        }
    if(flag==0)return false;
    else return true;
    }
    public String checkcode(String code){
       if(code.length()>=6&&code.length()<=10){
            return code;
        }else return "0";
    }
    public boolean againcode(String code,String againcode){
        if(code.equals(againcode)) return true;
        else return false;
    }
public boolean checkmail(String str) {
    int flag = 0;

    if (str.contains("@") && str.contains(".") && !str.startsWith("@") && !str.endsWith(".")) {
        if (str.indexOf(".") - str.indexOf("@") > 1 && str.indexOf("@") == str.lastIndexOf("@") && str.indexOf(".") == str.lastIndexOf(".")) {
            if (str.endsWith("cn") || str.endsWith("com")) {
                flag = 1;
            }

            }
        }
        if (flag == 1) return true;
        else return false;
}

public boolean checkphone(String str1){
    boolean boo1 = false;
    if(str1.startsWith("1")&&str1.length()==11) {
        boo1=true;
    }
   return boo1;
}
public String rdate(){
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    return df.format(new Date());
}
    public String vercode(){
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuffer sb = new StringBuffer() ;
        Random rd = new Random();
        for (int i = 0; i < 4; i++) {
            int start = rd.nextInt(base.length());
            String a = base.substring(start,start+1);
            sb.append(a);
        }
        return sb.toString();
    }

    public User findByName(String name){
        for(User user:list){
            if (user.name.equals(name)){
                return user;
            }
        }
        return null;
    }

    public boolean checkUserName(String name){
        boolean flag = false;
        for(User user:list){
            if (user.name.equals(name)){
                flag=true;
            }
        }
        return flag;
    }

    public boolean checkUserPassword(String userName,String userPassword){
        boolean flag = false;
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getName().equals(userName)&&list.get(i).getCode().equals(userPassword)){
                flag = true;
            }
        }
        return flag;
    }
}


